<?php
/**
 * 前段用户角色
 * Created by PhpStorm.
 * User: 
 * Date: 
 * Time: 
 */

namespace app\services\controller;

use think\captcha\Captcha;
use think\Config;
use think\Db;
use think\Request;
use think\Response;
use app\model\UserBase;
use ethink\mvc\BaseController;
use ethink\acl\Auth;
use think\Loader;

class Admin extends BaseController
{
    private $userId;
    private $roleId;
    public function __construct(Request $request = null)
    {
        $this->auth = new Auth(Config::get('api')['auth']);
        parent::__construct($request);
    }
    /**
     * [checkRole description]
     * @return [type] [description]
     */
    private function checkRole(){
      //1校验登陆状态
      $user = $this->checkUserStatus();  
      trace("admin,checkRole:".json_encode($user));
      if($user['status']){
          return $user['status'];
      }
      $this->userId = $user['session']['userId'];
      $this->roleId = $user['session']['roleId'];
      // var_dump($user);exit;
      //此控制器的所有权限都要求是管理员
      $import = $this->checkCaseRole($this->roleId,['admin']);
      trace("admin,checkCaseRole,ret:$import");
      return $import;
    }


     /**
     * [userList 查询所有用户]
     * @return [type] [description]
     */
    public function userList(){
        if($this->request->isGet()){
          $reqData = $this->request->get();
          trace("admin-userList,data:".json_encode($reqData));
          if(!is_array($reqData)){
              return $this->responseJson(null, $reqData);
          }         
          $userId = $this->userId;
          $roleId = $this->roleId;

          //2查询数据
          $Model = new \app\services\model\AdminUsers();
          $reqData['pageSize'] = Config::get('api')['list']['page_size'];
          $list = $Model->userList($userId,$reqData);
          $retData['data'] = $list->toArray();
          return $this->responseJson($retData, 0);   
        }
    } 
 
     /**
     * [userDetail 查询用户详情]
     * @return [type] [description]
     */
    public function userDetail(){
        $ret = $this->checkRole();
        if($ret){
          return $this->responseJson(null, $ret);          
        }
        if($this->request->isGet()){
          $reqData = $this->request->get();
          trace("admin-userDetail,data:".json_encode($reqData));
          if(!is_array($reqData)){
              return $this->responseJson(null, $reqData);
          }            
          if(empty($reqData['id'])){
              return $this->responseJson(null, 1002);            
          }
          
          
          $userId = $this->userId;
          $roleId = $this->roleId;
          //2查询数据
          $Model = new \app\services\model\AdminUsers();
          $list = $Model->userDetail($userId,$reqData);
          $retData['out_data'] = $list;
          return $this->responseJson($retData, 0);   
        }
    } 
 

     /**
     * [针对某个用户，重置密码]
     * @return [type] [description]
     */
    public function resetPassword(){
        $ret = $this->checkRole();
        if($ret){
          return $this->responseJson(null, $ret);          
        }
         if($this->request->isPost()){     
            $reqData = $this->getJsonData(['id']);
            trace("resetpassword,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }            
            $userId = $this->userId;
            $reqData['roleId'] = $this->roleId;
            //2查询数据
            $Model = new \app\services\model\AdminUsers();
            $reqData['initPassword'] = Config::get('api')['initPassword'];
            $retData = $Model->resetPassword($userId,$reqData);
            return $this->responseJson(null, $retData); 

        } 
    }   

     /**
     * [添加编辑用户 仅管理员有权限]
     * @return [type] [description]
     */
    public function addUser(){
        $ret = $this->checkRole();
        if($ret){
          return $this->responseJson(null, $ret);          
        }
         if($this->request->isPost()){     
            $reqData = $this->getJsonData(['account','tel','name','roleId']);
            trace("addUser,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }            
         
            $userId = $this->userId;
            //2查询数据
            $Model = new \app\services\model\AdminUsers();
            $reqData['initPassword'] = Config::get('api')['initPassword'];
            $retData = $Model->addUser($userId,$reqData);
            return $this->responseJson(null, $retData); 

        } 
    }  
     /**
     * [编辑用户 仅管理员有权限]
     * @return [type] [description]
     */
    public function updateUser($id){
        $ret = $this->checkRole();
        if($ret){
          return $this->responseJson(null, $ret);          
        }
          //1校验登陆状态
          $user = $this->checkUserStatus();  
          if($user['status']){
              return $this->responseJson(null, $user['status']);                
          }
            $userId = $this->userId;
            $reqData['roleId'] = $this->roleId;

         if($this->request->isPost()){     
            $reqData = $this->getJsonData(['account','tel','name','roleId']);
            trace("updateUser,id:$id,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }            
            //2查询数据
            $Model = new \app\services\model\AdminUsers();
            $reqData['initPassword'] = Config::get('api')['initPassword'];
            $reqData['id'] = $id;
            // var_dump($reqData);exit;
            $retData = $Model->addUser($userId,$reqData);
            return $this->responseJson(null, $retData); 

        } 
        else if($this->request->isDelete()){
            trace("updateUser,delete,id:$id,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }
          
            //2查询数据
            $Model = new \app\services\model\AdminUsers();
            $reqData['id'] = $id;
            // var_dump($reqData);exit;
            $retData = $Model->delUser($userId,$reqData);
            return $this->responseJson(null, $retData); 

        }
    }   

    /**
     * [删除 仅管理员有权限]
     * @return [type] [description]
     */
    public function delUser(){
        $ret = $this->checkRole();
        if($ret){
          return $this->responseJson(null, $ret);          
        }
         if($this->request->isPost()){     
            $reqData = $this->getJsonData(['id']);
            trace("delUser,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }            
            //1校验登陆状态
            $user = $this->checkUserStatus();  
            if($user['status']){
                return $this->responseJson(null, $user['status']);                
            }
            $userId = $this->userId;
            $reqData['roleId'] = $this->roleId;
            //2查询数据
            $Model = new \app\services\model\AdminUsers(); 
            $retData = $Model->delUser($userId,$reqData);
            return $this->responseJson(null, $retData); 

        } 
    }   

    


     /**
     * [roles 查询角色]
     * @return [type] [description]
     */
    public function roles(){
        if($this->request->isGet()){
          //查询
          $reqData = $this->request->get();
          trace("admin-roles,data:".json_encode($reqData));
          if(!is_array($reqData)){
              return $this->responseJson(null, $reqData);
          }            
          //1校验登陆状态
          $user = $this->checkUserStatus();  
          if($user['status']){
              return $this->responseJson(null, $user['status']);
          }
          $userId = $this->userId;
          $roleId = $this->roleId;
          //2查询数据
          $Model = new \app\services\model\AdminRoles();
          // $reqData['pageSize'] = Config::get('api')['list']['page_size'];
          $list = $Model->roleAll($userId,$reqData);
          $retData['data'] = $list;
          return $this->responseJson($retData, 0);   
        }
        else if($this->request->isPost()){
          
        $ret = $this->checkRole();
        if($ret){
          return $this->responseJson(null, $ret);          
        }
            //添加角色
            $reqData = $this->getJsonData(['title','permissions']);
            trace("roles,add role,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }            
            //1校验登陆状态
            $user = $this->checkUserStatus();  
            if($user['status']){
                return $this->responseJson(null, $user['status']);                
            }
            $userId = $user['session']['userId'];
            $reqData['roleId'] = $user['session']['roleId'];
            //2查询数据
            $Model = new \app\services\model\AdminRoles();
            $retData = $Model->addRole($userId,$reqData);
            return $this->responseJson(null, $retData); 

        }
    } 


    /**
     * [所有权限、编辑添加权限]
     * @return [type] [description]
     */
    public function permissions(){
        if($this->request->isGet()){
          $reqData = $this->request->get();
          trace("role-permissions,data:".json_encode($reqData));
          if(!is_array($reqData)){
              return $this->responseJson(null, $reqData);
          }            
        
          $userId = $this->userId;
          $roleId = $this->roleId;

          //2查询数据
          $Model = new \app\services\model\AdminPermissions();
          $list = $Model->permissionsAll();
          // var_dump($list);exit;
          $retData['data'] = $list;
          return $this->responseJson($retData, 0);                      
        }      
        else if($this->request->isPost()){     

          $ret = $this->checkRole();
          if($ret){
            return $this->responseJson(null, $ret);          
          }
          $reqData = $this->getJsonData(['permissions']);
          trace("permissions post,data:".json_encode($reqData));

          if(!is_array($reqData)){
              return $this->responseJson(null, $reqData);
          }            
          // if(!$reqData['title'] || !$reqData['path']|| !$reqData['method']){
          //   return $this->responseJson(null,1002);
          // }
        
          $userId = $this->userId;
          $reqData['roleId'] = $this->roleId;
          //2查询数据
          $Model = new \app\services\model\AdminPermissions();
          $retData = $Model->addOnePermission($userId,$reqData);
          return $this->responseJson(null, $retData); 

      }
      else{
        trace("permissions,request type is error");
      }   
    }   

     /**
     * [所有权限、编辑添加权限]
     * @return [type] [description]
     */
    public function permissionsByRole($id){
         if($this->request->isGet()){
            $reqData = $this->request->get();
            trace("role-permissionsByRole,id:$id,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }            
            $userId = $this->userId;
            $roleId = $this->roleId;
            //2查询数据
            $Model = new \app\services\model\AdminPermissions();
            $list = $Model->permissionsByRoleid($id);
            $retData['data'] = $list;
            return $this->responseJson($retData, 0);                      
        }        
    }   


      /**
     * [roles 更新角色]
     * @return [type] [description]
     */
    public function updateRoles($id){
      $ret = $this->checkRole();
      if($ret){
          return $this->responseJson(null, $ret);          
      }
      $userId = $this->userId;
      $reqData['roleId'] = $this->roleId;
      if($this->request->isPost()){
            //添加角色
            $reqData = $this->getJsonData(['title','permissions']);
            trace("roles,updateRoles,id:$id,data:".json_encode($reqData));
            if(!is_array($reqData)){
                return $this->responseJson(null, $reqData);
            }            
            //2查询数据
            $Model = new \app\services\model\AdminRoles();
            $reqData['id'] = $id;
            $retData = $Model->addRole($userId,$reqData);
            return $this->responseJson(null, $retData); 

        }
        else if($this->request->isDelete()){
            //添加角色
            $reqData = $this->getJsonData([]);
            trace("roles,delete roles,id:$id,data:".json_encode($reqData));
            // if(!is_array($reqData)){
            //     return $this->responseJson(null, $reqData);
            // }            
            
            //2查询数据
            $Model = new \app\services\model\AdminRoles();
            $retData = $Model->deleteRole($userId,$id);
            return $this->responseJson(null, $retData); 
        }
    } 

}